import { createRouter, createWebHashHistory } from "vue-router";
import car from "@/pages/car/index.vue";
import login from "@/pages/car/login/index.vue";
import oldmanLogin from "@/pages/oldman/login/index.vue";

const baseRouteName: "car" | "oldman" = import.meta.env.VITE_APP_CAR_OR_OLDMAN;

// oldman那个项目，直接新建一个路由文件
const routes = [
  {
    path: "/",
    name: baseRouteName,
    redirect: `/${baseRouteName}/car-mall`,
  },
  {
    path: `/${baseRouteName}`,
    name: baseRouteName,
    component: car,
    children:
      baseRouteName === "car"
        ? [
            {
              path: "login",
              name: "login",
              component: login,
            },
            {
              path: "community",
              name: "community",
              component: () => import("@/pages/car/community/index.vue"),
            },
            {
              path: "car-mall",
              name: "car-mall",
              component: () => import("@/pages/car/car-mall/index.vue"),
            },
            {
              path: "order-management",
              name: "order-management",
              component: () => import("@/pages/car/order-management/index.vue"),
            },
          ]
        : [
            {
              path: "login",
              name: "login",
              component: oldmanLogin,
            },
            {
              path: "health",
              name: "health",
              component: () =>
                import("@/pages/oldman/health-management/index.vue"),
            },
            {
              path: "community",
              name: "community",
              component: () => import("@/pages/oldman/community/index.vue"),
            },
            {
              path: "trip",
              name: "trip",
              component: () =>
                import("@/pages/oldman/trip-management/index.vue"),
            },
            {
              path: "consultation",
              name: "consultation",
              component: () => import("@/pages/oldman/consultation/index.vue"),
            },
            {
              path: "yanglaoyuan",
              name: "yanglaoyuan",
              component: () => import("@/pages/oldman/yanglaoyuan/index.vue"),
            },
            {
              path: "hugong",
              name: "hugong",
              component: () => import("@/pages/oldman/hugong/index.vue"),
            },
            {
              path: "activities",
              name: "activities",
              component: () => import("@/pages/oldman/activities/index.vue"),
            },
            {
              path: "medicine",
              name: "medicine",
              component: () =>
                import("@/pages/oldman/medicine-management/index.vue"),
            },
          ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes as any,
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("jwtToken");
  const whiteList = [`/${baseRouteName}/login`];

  if (!token) {
    // 如果要去的路由不在白名单内，则重定向到登录页
    if (!whiteList.includes(to.path)) {
      next(`/${baseRouteName}/login`);
    } else {
      // 白名单页面直接放行
      next();
    }
  } else {
    next();
  }
});

export default router;
